; example2.nsi
;
; This script is based on example1.nsi but it remembers the directory,
; has uninstall support and (optionally) installs start menu shortcuts.
;
; It will install example2.nsi into a directory that the user selects.
;
; See install-shared.nsi for a more robust way of checking for administrator rights.
; See install-per-user.nsi for a file association example.

;--------------------------------

!define VERSION "0.9.5"

;!include "MUI2.nsh"

LoadLanguageFile "${NSISDIR}\Contrib\Language files\SimpChinese.nlf"

; The name of the installer
Name "Haishen-Print"

; The file to write
OutFile "haishen_print_${VERSION}.exe"
;OutFile "haishen_print_win7_${VERSION}.exe"

; Request application privileges for Windows Vista and higher
RequestExecutionLevel admin

; Build Unicode installer
Unicode True

; The default installation directory
InstallDir $PROGRAMFILES64\HaishenPrint

; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\WuJinHai\HaishenPrint" "Install_Dir"


ShowInstDetails show

;--------------------------------

; Pages

Page components
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

;--------------------------------

; The stuff to install
Section "海神打印服务"

	SectionIn RO

	; Set output path to the installation directory.
	SetOutPath $INSTDIR

	; Put file there
	File "gsapi.py"
	File "config.py"
	File "server.py"
	File "startup.bat"
	File /r "python"

	; Write the installation path into the registry
	WriteRegStr HKLM "Software\WuJinHai\HaishenPrint" "Install_Dir" "$INSTDIR"

	; Write the uninstall keys for Windows
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\HaishenPrint" "DisplayName" "海神打印服务"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\HaishenPrint" "UninstallString" '"$INSTDIR\uninstall.exe"'
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\HaishenPrint" "NoModify" 1
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\HaishenPrint" "NoRepair" 1
	WriteUninstaller "$INSTDIR\uninstall.exe"

	IfFileExists $LOCALAPPDATA\haishen_print.ini configdone
		FindFirst $0 $1 "$PROGRAMFILES64\gs\*"
		loop:
			StrCmp $1 "" done
			StrCmp $1 "." +3
			StrCmp $1 ".." +2
			StrCpy $2 "$PROGRAMFILES64\gs\$1\bin\gsdll64.dll"
			FindNext $0 $1
			Goto loop
		done:
		FindClose $0

	StrCmp $2 "" configdone
		DetailPrint $2
		WriteINIStr $LOCALAPPDATA\haishen_print.ini "Ghostscript" "dll" "$2"
	configdone:

SectionEnd

; Optional section (can be disabled by the user)
Section "开始菜单项"

	CreateDirectory "$SMPROGRAMS\HaishenPrint"
	CreateShortcut "$SMPROGRAMS\HaishenPrint\卸载.lnk" "$INSTDIR\uninstall.exe"
	CreateShortcut "$SMPROGRAMS\HaishenPrint\启动.lnk" "$INSTDIR\startup.bat"

SectionEnd

; Optional section (can be disabled by the user)
Section "自动启动"

	CreateShortcut "$SMSTARTUP\海神打印服务.lnk" "$INSTDIR\startup.bat"

SectionEnd

;--------------------------------

; Uninstaller

Section "Uninstall"

	; Remove registry keys
	DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\HaishenPrint"
	DeleteRegKey HKLM "Software\WuJinHai\HaishenPrint"

	; Remove files and uninstaller
	;Delete $INSTDIR\*
	;RMDir /r $INSTDIR\python
	Delete $LOCALAPPDATA\haishen_print.ini

	; Remove shortcuts, if any
	Delete "$SMPROGRAMS\HaishenPrint\*.lnk"
	Delete "$SMSTARTUP\海神打印服务.lnk"

	; Remove directories
	RMDir "$SMPROGRAMS\HaishenPrint"
	RMDir /r /REBOOTOK "$INSTDIR"

SectionEnd